package cn.com.gridinfo.sooc.core.my.note.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toolbar;

import com.jeremy.arad.utils.AnimUtil;
import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.my.commen.CodeConst;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.core.my.note.adapter.MyNoteListAdapter;
import cn.com.gridinfo.sooc.core.my.note.bean.MyNote;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 我的笔记
 * Created by 刘晓晨 on 2015/12/16.
 */
public class MyNoteActivity extends BaseSoocActivity {
    @InjectView(R.id.course_main_list)
    UltimateRecyclerView courseMainList;//列表
    MyDao dao; //我的dao
    LinearLayoutManager linearLayoutManager;// 列表管理器
    MyNoteListAdapter myNoteListAdapter;
    int pageNo = 1;
    int pageSize = 10;
    int maxLastPositon;//数据最后位置
    public List<MyNote> myNoteList;
    private static final int INTENT_NOTEDETAIL = 13;//跳转到笔记详情页的常量
    private int intent_position = -1;//要跳转的位置
    @InjectView(R.id.blank_view)
    RelativeLayout blankRe;//空白页布局
    @InjectView(R.id.tipBlank)
    TextView tipBlank;//我的笔记空白页提示

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_note_main_list);
        ButterKnife.inject(this);
        getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.MATCH_PARENT);
        dao = new MyDao(this);
        dao.myNote(pageNo, CodeConst.NOTELIST_FIRST);//请求接口
        linearLayoutManager = new LinearLayoutManager(this);
        courseMainList.setLayoutManager(linearLayoutManager);
        tipBlank.setText("还没有写过笔记哦~");
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return "我的笔记";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //点击回退
    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        finish();
    }

    //请求成功
    @Override
    public void onRequestSuccess(int requestCode) {
        super.onRequestSuccess(requestCode);
        try {
            if (dao.status.equals("0")) {
                switch (requestCode) {
                    //课程首次请求
                    case CodeConst.NOTELIST_FIRST:
                        initNoteList();
                        if (dao.getMyNoteList() != null && !dao.getMyNoteList().isEmpty()) {
                            courseMainList.hideEmptyView();
                        } else {
                            //无数据处理
                            //空白页布局显示
                            blankRe.setVisibility(View.VISIBLE);
                            courseMainList.showEmptyView();
                        }

                        break;
                    //课程刷新操作
                    case CodeConst.NOTELIST_PUSHDOWN:
                        courseMainList.setRefreshing(false);
                        if (dao.getMyNoteList() != null && !dao.getMyNoteList().isEmpty()) {
                            courseMainList.hideEmptyView();
                        } else {
                            //无数据处理
                            //空白页布局显示
                            blankRe.setVisibility(View.VISIBLE);
                            courseMainList.showEmptyView();
                        }
                        //刷新数据
                        myNoteListAdapter.refresh(dao.getMyNoteList());
                        if (dao.getMyNoteList().size() > (pageSize - 1))
                            //数据大于每页显示的条数,则重新开启加载更多
                            myNoteListAdapter.loadMoreDataView();
                        else {
                            //数据小于当前页的话则不显示加载更多
                            myNoteListAdapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    //课程翻页操作
                    case CodeConst.NOTELIST_PULLUP:
                        if (dao.getMyNoteList() != null && !dao.getMyNoteList().isEmpty()) {
                            //加载更多
                            myNoteListAdapter.loadmore(dao.getMyNoteList());

                        } else {
                            //无数据则不显示下一页
                            myNoteListAdapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.statusCode);
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    /**
     * 初始化 list 数据 分页
     */
    private void initNoteList() {
        myNoteListAdapter = new MyNoteListAdapter(dao.getMyNoteList(), this);
        courseMainList.setAdapter(myNoteListAdapter);
        courseMainList.enableLoadmore();
        courseMainList.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //下拉
                pageNo = 1;
                dao.myNote(pageNo, CodeConst.NOTELIST_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        courseMainList.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {

                if (itemsCount > (pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.myNote(++pageNo, CodeConst.NOTELIST_PULLUP);
                } else {
                    myNoteListAdapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }
        });
        //listview的单项点击
        myNoteListAdapter.setmOnItemClickLitener(new MyNoteListAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(MyNote myNote, int position) {
                //跳转到笔记详情页面
                intent_position = position;
                Intent intent = new Intent(MyNoteActivity.this, NoteDetailActivity.class);
                intent.putExtra("kcid", myNote.getKcid());//将课程id传出
                intent.putExtra("kcmc", myNote.getKcmc());// 将课程名称传出
                startActivityForResult(intent, INTENT_NOTEDETAIL);
                AnimUtil.intentSlidIn(MyNoteActivity.this);
            }
        });
    }

    /**
     * 数据回填
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        Log.d("onActivityResult");
        switch (requestCode) {
            case INTENT_NOTEDETAIL:
                //从笔记详情页面返回
                if (data != null) {
                    int size = data.getIntExtra("size", -1);
                    Log.d("size=" + size);
                    switch (size) {
                        case 0:
                            //删除本条
                            myNoteListAdapter.deleteByPosition(intent_position);
                            break;
                        case -1:
                            Log.d("返回数据有问题");
                            break;
                        default:
                            //修改本条数据
                            myNoteListAdapter.updateByPosition(intent_position, size);
                            break;
                    }
                }
                break;
        }
    }
}
